﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using EntitiesModel;
using IMCModel;
using DPModel;

namespace ITMan
{
    public partial class FormDPSync : Form
    {
        public FormDPSync()
        {
            InitializeComponent();
        }

        private void btSync_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(@"停止使用！");
            //goto END;
            System.Threading.Thread thread = new Thread(StartWorkThread);
            thread.IsBackground = true;
            thread.Start();
            //END:
            ;
        }

        private void StartWorkThread()
        {
            if (this.InvokeRequired)
            {
                BeginInvoke(new EventHandler(dpSync), null);
            }
            else
            {
                dpSync(this, null);
            }
        }

        private void dpSync(object sender,EventArgs e)
        {
            System.Threading.Thread.Sleep(2000);
            var imcConText = new EadEntitiesModel();

            //读取用户、MAC资料
            IQueryable<VW_MacToDP> imcUser = imcConText.VW_MacToDPs;
            WriteLog("SYS\t加载IMC用户资料！");

            foreach (var imc in imcUser)
            {
                //WriteLog(string.Format("SYS\t开始处理用户{0}", imc.USER_NAME));
                dpExists(imc);
            }
            WriteLog("SYS\t同步完成！");
            MessageBox.Show(string.Format("同步完成！"));
        }

        private void dpExists(VW_MacToDP imcUser)
        {
            var dpContext = new DPEntities();
            IQueryable<Dp_sys_org_user> dpUsers = from u in dpContext.Dp_sys_org_users
                       where u.MAC == imcUser.MAC_ADDRESS
                       select u;
            if (dpUsers.Any())
            {
                Dp_sys_org_user dpUser = dpUsers.FirstOrDefault();
                if (dpUser.ACCOUNTS != imcUser.USER_NAME)
                {
                    //更新
                    WriteLog(string.Format("U\tMAC地址{0}由用户{1}:{2}变更为{3}:{4}", dpUser.MAC, dpUser.ACCOUNTS, dpUser.NAME, imcUser.USER_NAME, imcUser.FULL_NAME));
                    dpUser.ACCOUNTS = imcUser.USER_NAME;
                    dpUser.NAME = imcUser.FULL_NAME;
                    dpContext.SaveChanges();

                    
                }
            }
            else
            {
                //插入
                Dp_sys_org_user dpUser =new Dp_sys_org_user();
                dpUser.NAME = imcUser.FULL_NAME;
                dpUser.MAC = imcUser.MAC_ADDRESS;
                dpUser.PART = 0;
                dpUser.ACCOUNTS = imcUser.USER_NAME;
                dpUser.VLAN = 0;

                dpContext.Add(dpUser);
                dpContext.SaveChanges();

                WriteLog(string.Format("I\tMAC地址{0}绑定至用户{1}:{2}", dpUser.MAC, dpUser.ACCOUNTS, dpUser.NAME));
            }

        }

        private void WriteLog(string message)
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory;
            if (!Directory.Exists(logPath + @"\log"))
            {
                Directory.CreateDirectory(logPath + @"\log");
            }
            tbLog.AppendText(string.Format("{0}\t" + message + System.Environment.NewLine, System.DateTime.Now));
            tbLog.Refresh();

            //更新tbLog中显示内容
            StreamWriter dout = new StreamWriter(string.Format(logPath + @"\log\{0}.log", DateTime.Today.ToString("yyyy-MM-dd")), true);
            dout.Write("{0}\t" + message + System.Environment.NewLine, System.DateTime.Now);
            dout.Close();
        }
    }
}
